package com.sneakxy.cloudbase.platform.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.sneakxy.cloudbase.platform.bean.AccountRole;
import com.sneakxy.cloudbase.platform.bean.AccountRoleId;
import com.sneakxy.cloudbase.platform.repository.AccountRoleRepository;

/**
 * 
 * @author 潜行的青衣
 * @see
 */
@Service
public class AccountRoleService extends BaseService<AccountRole, AccountRoleId, AccountRoleRepository> implements IAccountRoleService {

	@Value("${merge.size.account}")
	private int mergeSize;
	
	@Override
	public String getTableName(String accountId) {
		return getTableName(accountId, mergeSize);
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.cloudbase.platform.service.IAccountRoleService#findRoles(java.lang.String)
	 */
	@Override
	public List<String> findRoles(String accountId) {
		return this.getRepository().findRoles(accountId, this.getTableName(accountId, mergeSize));
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.cloudbase.platform.service.IAccountRoleService#deleteByRoleId(java.lang.String)
	 */
	@Override
	public int deleteByRoleId(String roleId) {
		return this.getRepository().deleteByRoleId(roleId);
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.cloudbase.platform.service.IAccountRoleService#deleteByAccountId(java.lang.String)
	 */
	@Override
	public int deleteByAccountId(String accountId) {
		return this.getRepository().deleteByAccountId(accountId, this.getTableName(accountId, mergeSize));
	}

	@Override
	public Iterable<AccountRole> saveSplitTable(Iterable<AccountRole> entities) {
		String tabelName = null;
		for(AccountRole entity: entities) {
			tabelName = this.getTableName(entity.getId().getAccountId(), mergeSize);
			this.save(entity, tabelName);
		}
		return entities;
	}
	
}
